<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Common\Model;

/**
 * Description of BookCateModel
 *
 * @author gangguo
 */
class SiteConfigModel extends XuecheModel{
    //put your code here
    protected $pk = 'name';
    protected $tableName = 'site_config';
    protected $fields = array(
            'name', 'value','disabled'
        );

    /**
     * 清空数据缓存
     *
     */
    public function ClearAllCache() {

    }
    public function Validate($data) {
        return true;
    }
    static public function getObject(){
        return new self();
    }

    //取值
    static public function getValue($name){
        if($name && $value=self::getObject()->where("`name`='{$name}'")->field('value')->find()){
            return json_decode($value['value']);
        }
        return false;
    }

    //存值
    static public function saveValue($data){
        if(empty($data['name']) || empty($data['value'])){
            return false;
        }
        $this = self::getObject();
        $data['value'] = json_encode($data['value']);
        if($this->where("`name`='{$data['name']}'")->find()){
            return $this->where("`name`='{$data['name']}'")->save($data);
        }else{
            return $this->add($data);
        }
    }
}
